package com.example.shop.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.shop.pojo.Goods;
import com.example.shop.pojo.GoodsClass;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface GoodsClassMapper extends BaseMapper<GoodsClass> {

    @Select("select class_price from r_goods_class where goods_id = ${goodsId} and goods_class = #{goodsClass}")
    int selectPrice(@Param("goodsId") long goodsId,@Param("goodsClass") String goodsClass);


    @Insert("<script>" +
            "insert r_goods_class(goods_id,goods_class_picture,goods_class,class_price,class_inventory) " +
            "VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(${item.goodsId},#{item.goodsClassPicture},#{item.goodsClass},${item.classPrice},${item.classInventory})" +
            "</foreach>" +
            "</script>")
    int insertList(@Param("list") List<GoodsClass> list);

    @Delete("delete from r_goods_class gc " +
            "where " +
            "gc.goods_id = (select g.goods_id from goods g where g.goods_id = ${goodsId} and g.merchant_id = ${merchantId}) " +
            "and " +
            "gc.goods_class = #{goodsClass}" +
            "")
    int delete(@Param("merchantId")long merchantId,@Param("goodsId") long goodsId,@Param("goodsClass") String goodsClass);


    @Update("<script>" +
            "update r_goods_class set class_inventory = " +
            "CASE goods_class " +
            "<foreach collection='inventoryMap' item='value' index='key' separator=' ' close=' '>" +
                "when #{key} then ${value}" +
            "</foreach>" +
            "END " +
            "where goods_id = ${goodsId}" +
            "</script>")
    int updateInventory(@Param("goodsId") long goodsId,@Param("inventoryMap") Map<String, Integer> inventoryMap);
}
